<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/7/31
 * Time: 下午4:39
 */

namespace Common\Service;
use Aliyun\DySDKLite\SignatureHelper;

class ThirdpartService extends BaseService
{

    public function uploadpicByPath($filePath,$ext) {
        require_once  VENDOR_PATH.'qiniu/autoload.php';
        // 用于签名的公钥和私钥
        $accessKey = config('qiniu_accesskey');
        $secretKey = config('qiniu_secretkey');
        $bucket = config('qiniu_bucket');
        $domain = config('qiniu_domain');
        $file = request()->file('file');
        // 要上传图片的本地路径
        //$filePath = $file->getRealPath();
        // 上传到七牛后保存的文件名
        $key =substr(md5($filePath) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $auth=new \Qiniu\Auth($accessKey, $secretKey);
        // 要上传的空间
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new \Qiniu\Storage\UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        $file=ROOT_PATH."public/".$filePath;
        list($ret, $err) = $uploadMgr->putFile($token,$filePath, $file);
        if ($err !== null) {
            return array('code'=>202,'msg'=>'上传失败','data'=>$err);
        } else {
            return array('code'=>200, 'msg'=>'上传成功','data'=>$domain.$ret['key']);
        }
    }


    public function unlink($path)
    {
        return is_file($path) && unlink($path);
    }

    /**
     * /**
     * 签名：金江海
      模板ID：SMS_141415059
      id -- key
      LTAIIY2wdcpXslTl  9T6Bd91YN9kAZbY8Z9WYaXP4W4F2Xq
     * @param $phone
     * @param $content
     * @param $config
     * @param $tempCode
     * @return bool|\stdClass
     */
    public function smsSend($phone,$content,$tempCode='SMS_6280751',$config=array()){
        vendor('alimsg.SignatureHelper');
        $signature=new SignatureHelper();
        $params=array(
            'PhoneNumbers'=>$phone,
            'SignName'=>C('ALIDAYU_SIGN'),
            'TemplateCode'=>C('ALIDAYU_SHENFEN_TMP'),
            'TemplateParam'=>$content,// fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        );
        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }
        if(empty($config)){
            $accessKeyId='LTAIf2zRmxxWeODT';
            $accessKeySecret='K6cmDfbi4Mddrm6yKTbm8xp1PNN5dK';
        }else{
            $accessKeyId=C('ALIDAYU_APPKEY');//'LTAIf2zRmxxWeODT';
            $accessKeySecret=C('ALIDAYU_APPSEC');//'K6cmDfbi4Mddrm6yKTbm8xp1PNN5dK';
        }
        $domain="dysmsapi.aliyuncs.com";
        $params=array_merge($params, array(
            "RegionId" => "cn-hangzhou",
            "Action" => "SendSms",
            "Version" => "2017-05-25",
        ));
        $re=$signature->request($accessKeyId,$accessKeySecret,$domain,$params);
        $addSmsLog['phone']=$phone;
        $addSmsLog['create_time']=date('Y-m-d H:i:s');
        $addSmsLog['data']=$re;
        //发送了短信要记录数据库
        if($re['Code']=='OK'){
            $addSmsLog['status']=1;
            return true;
        }else{
            $addSmsLog['status']=0;
            return_json(1,$re['Message']);
            return false;
        }
    }

}